package cn.wwcyc.service.impl;

import cn.wwcyc.domain.TjReg;
import cn.wwcyc.mapper.TjRegMapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import cn.wwcyc.domain.TjDev;
import cn.wwcyc.service.TjDevService;
import cn.wwcyc.mapper.TjDevMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
* @author ad
* @description 针对表【TJ_DEV】的数据库操作Service实现
* @createDate 2024-05-02 21:22:09
*/
@Service
public class TjDevServiceImpl extends ServiceImpl<TjDevMapper, TjDev>
    implements TjDevService{
    @Autowired
    private TjDevMapper tjDevMapper;
    @Autowired
    private TjRegMapper tjRegMapper;

    @Transactional
    @Override
    public void copy(Long id, Integer count) {
        TjDev tjDev = getById(id);
        QueryWrapper<TjReg> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("devid",id);
        List<TjReg> listReg =  tjRegMapper.selectList(queryWrapper);
        for (int i = 0; i < count; i++) {
            TjDev tjDev1 = new TjDev();
            tjDev1.setName(tjDev.getName() + "_copy_" + (i+1));
            tjDev1.setIp(tjDev.getIp());
            tjDev1.setDevtype(tjDev.getDevtype());
            tjDev1.setDwid(tjDev.getDwid());
            tjDevMapper.insertDev(tjDev1); //自动返回主键id，并存入tjDev1对象中
//            QueryWrapper<TjDev> queryWrapper1 = new QueryWrapper<>();
//            queryWrapper1.eq("name",tjDev1.getName());
//            tjDev1 = getOne(queryWrapper1);
            System.out.println("-------------------" + tjDev1.getId());
            for(TjReg tjReg:listReg){
                TjReg tjReg1 = new TjReg();
                tjReg1.setDevid(tjDev1.getId());
                tjReg1.setName(tjReg.getName());
                tjReg1.setLocation(tjReg.getLocation());
                tjReg1.setRegdatatp(tjReg.getRegdatatp());
                tjReg1.setRegtype(tjReg.getRegtype());
                tjReg1.setSpecialtypeid(tjReg.getSpecialtypeid());
                tjReg1.setDevid(tjDev1.getId());
                tjRegMapper.insert(tjReg1);
            }
        }
    }
}




